# 先构建项目
FROM golang:1.20.5-alpine as builder            
# 这里在docker里也使用go module的代理服务   
ENV GOPROXY https://goproxy.cn,direct
ENV GO111MODULE on
# 在容器根目录 创建一个 apps 目录
RUN mkdir -p /apps
# 设置工作目录
WORKDIR /apps
COPY . /apps
## 编译
RUN go build -o /apps/main ./main.go
 
# ============================================================
# ============================================================
# ======================= 运行应用 ============================
# ============================================================
# ============================================================
 
# 表示依赖 alpine 最新版
FROM alpine:latest

# 下载Alpine镜像很慢有时候会卡住，因为使用的资源默认是境外服务器，将境外服务器替换为国内服务器即可
# RUN set -eux && sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
# LABEL MAINTAINER="Joker"
# LABEL EMAIL="xxxx@qq.com"

ENV VERSION 1.0
# 在容器根目录 创建一个 apps 目录
RUN mkdir -p /apps
# 设置工作目录
WORKDIR /apps
# 挂载容器目录
VOLUME ["/apps/config"]
VOLUME ["/apps/logs"]
 
# 可执行文件拷贝当前目录下
COPY --from=builder /apps/main /apps/main
 
# 拷贝配置文件到容器中
# COPY --from=builder /apps/config /apps/config
# COPY --from=builder logs /apps/logs
 
# 设置时区为上海
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
 
# 设置时区（以 Asia/Shanghai 为例）
RUN apk add --no-cache tzdata
ENV TZ=Asia/Shanghai
# 设置编码
ENV LANG C.UTF-8
 
# 暴露端口
EXPOSE 8080
 
# 运行golang程序的命令
CMD ["./main"]


# 来源:https://blog.csdn.net/qq_39272466/article/details/131212322